package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scsj.supermarket.R;

import java.util.List;

/**
 * 地址列表编辑
 */

public class AdressEditAdapter extends RecyclerView.Adapter<AdressEditAdapter.MyViewHolder> {
    private Context context;
    private List<String> data;
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    private MyViewHolder holder;//
    private int layoutPosition;
    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data, int position);
    }
    public AdressEditAdapter(Context context, List<String> data){
        this.context = context;
        this.data = data;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//
//        if(viewType>2){
//            View itemView = View.inflate(context, R.layout.item_adress_edit_add, null);
//            holder = new MyViewHolder(itemView);//
//        }else {
            View itemView = View.inflate(context, R.layout.item_adress_edit, null);
            holder = new MyViewHolder(itemView);//
//        }

        return holder;
    }

    @Override

    public void onBindViewHolder(final MyViewHolder holder,final int position) {//
        holder.textView.setText(data.get(position));//
        holder.itemView.setTag(data.get(position));//
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前点击的位置
                layoutPosition = holder.getLayoutPosition();
                notifyDataSetChanged();
                mOnItemClickListener.onItemClick(holder.itemView, (String) holder.itemView.getTag(), layoutPosition);
            }
        });

        //更改状态
        if(position == layoutPosition){
            holder.textView.setBackgroundResource(R.drawable.lable_text_side_line_select);//
//            holder.textView.setTextColor(Color.BLUE);
        }else{
            holder.textView.setBackgroundResource(R.drawable.lable_text_side_line_unselect);//
//            holder.textView.setTextColor(Color.BLUE);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolderFresh extends RecyclerView.ViewHolder {
       // private final TextView textView;
        public MyViewHolderFresh(View itemView) {
            super(itemView);
           //textView = itemView.findViewById(R.id.edit_add_lable_add_tv);
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.adress_edit_tv);
        }
    }
}
